package com.pricl.wechat.faq.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.soecode.wxtools.api.IService;
import com.soecode.wxtools.api.WxService;

/**
 * 验证微信服务器
 * @author winton
 *
 */
@Controller
@RequestMapping(value = "/wechat")
public class WeChatController {
	
	private IService iService = new WxService();
	
	@RequestMapping(value = "/check")
	public void checkWechat(HttpServletRequest request, HttpServletResponse response) {
		// 验证服务器的有效性
		try (PrintWriter out = response.getWriter()) {
			String signature = request.getParameter("signature");
			String timestamp = request.getParameter("timestamp");
			String nonce = request.getParameter("nonce");
			String echostr = request.getParameter("echostr");
			if (iService.checkSignature(signature, timestamp, nonce, echostr)) {
				out.print(echostr);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
